import time

from common.BasePages import BasePage


class GatewayPage(BasePage):
    # 网关管理
    def gateway_url(self):
        self.goto_url("projectManagement/gatewayManagement")

    def create_gateway(self):
        self.page.get_by_role("button", name="新增").click()
        self.page.get_by_role("dialog", name="网关新增窗口").get_by_placeholder("请输入项目名称").click()
        self.page.get_by_role("option", name="张院龙").click()
        self.page.get_by_role("textbox", name="请输入网关别名").click()
        self.page.get_by_role("textbox", name="请输入网关别名").fill("测试网关")
        self.page.get_by_role("dialog", name="网关新增窗口").get_by_placeholder("请输入网关SN").click()
        self.page.get_by_role("dialog", name="网关新增窗口").get_by_placeholder("请输入网关SN").fill("30100B14165BCC38")
        self.page.get_by_role("textbox", name="请输入备注").click()
        self.page.get_by_role("button", name="确 定").click()
        self.assert_alert_content("新增网关成功")

    def edit_gateway(self):
        self.page.locator(
            ".el-table__fixed-body-wrapper > .el-table__virtual-wrapper > div > .el-table__body > tbody > tr > .el-table_6_column_87 > .cell > .basicTableBtnBox > .el-icon-edit-outline").first.click()
        self.page.get_by_role("dialog", name="网关编辑窗口").get_by_placeholder("请输入上传周期").click()
        self.page.get_by_role("dialog", name="网关编辑窗口").get_by_placeholder("请输入上传周期").fill("7")
        self.page.get_by_role("button", name="确 定").click()
        self.assert_alert_content("编辑网关成功，请及时下发信息")

    def delete_gateway(self):
        self.page.locator(
            ".el-table__fixed-body-wrapper > .el-table__virtual-wrapper > div > .el-table__body > tbody > tr > .el-table_6_column_87 > .cell > .basicTableBtnBox > .el-icon-delete").first.click()
        self.page.get_by_role("button", name="确认").click()
        self.assert_alert_content("删除网关成功")

    def bid(self):
        """
        基础信息下发
        :return:
        """
        self.click_more_functions("网关管理", "基础信息下发")
        self.page.get_by_role("button", name="确认").click()
        self.page.get_by_text("基础信息下发成功")

    def point_tab_issued(self):
        """
        点表信息下发
        :return:
        """
        time.sleep(2)
        self.click_more_functions("网关管理", "点表下发")
        self.page.get_by_role("button", name="确认").click()
        self.page.get_by_text("点表下发成功")

    def data_issued(self):
        self.click_more_functions("网关管理", "实时数据")
        self.page.get_by_role("rowgroup").filter(has_text="下发下发下发").get_by_role("button").first.click()
        time.sleep(1)
        self.page.locator(f"li:has-text('下发')").last.click()
        self.page.get_by_role("textbox", name="请输入下发数值").click()
        self.page.get_by_role("textbox", name="请输入下发数值").fill("1")
        self.page.get_by_role("button", name="确 定").click()
        self.assert_alert_content("下发成功")

    def data_issued_io(self):
        self.click_more_functions("网关管理", "实时数据")
        # 第一遍下发
        self.page.get_by_role("rowgroup").filter(has_text="下发下发下发").get_by_role("button").nth(1).click()
        time.sleep(1)
        self.page.locator(f"li:has-text('下发')").last.click()
        self.page.get_by_role("dialog", name="请输入值").get_by_placeholder("请选择").click()
        self.page.get_by_role("listitem").filter(has_text="true").click()
        self.page.get_by_role("button", name="确 定").click()
        # 第二遍下发
        self.page.get_by_role("rowgroup").filter(has_text="下发下发下发").get_by_role("button").nth(1).click()
        time.sleep(1)
        self.page.locator(f"li:has-text('下发')").last.click()
        self.page.get_by_role("dialog", name="请输入值").get_by_placeholder("请选择").click()
        self.page.get_by_role("listitem").filter(has_text="false").click()
        self.page.get_by_role("button", name="确 定").click()
        # 第三遍下发
        self.page.get_by_role("rowgroup").filter(has_text="下发下发下发").get_by_role("button").nth(1).click()
        time.sleep(1)
        self.page.locator(f"li:has-text('下发')").last.click()
        self.page.get_by_role("dialog", name="请输入值").get_by_placeholder("请选择").click()
        self.page.get_by_role("listitem").filter(has_text="true").click()
        self.page.get_by_role("button", name="确 定").click()
